Pandas 时间序列—date_range函数

函数原型:

pandas.date_range(    start=None, 
                    end=None, 
                    periods=None,
                    freq='D', 
                    tz=None, 
                    normalize=False,
                    name=None, 
                    closed=None, 
                    **kwargs
                 )

参数:

  • start:string 或 datetime-like,默认值是 None,表示日期的起点。
  • end:string 或 datetime-like,默认值是 None,表示日期的终点。
  • periods:integer 或 None,默认值是None,表示你要从这个函数产生多少个日期索引值;如果是None的话,那么start和end必须不能为None。
  • freq:string 或 DateOffset,默认值是 ’D’,表示以自然日为单位,这个参数用来指定计时单位,比如 ’5H’ 表示每隔5个小时计算一次。
  • tz:string 或 None,表示时区,例如:’Asia/Hong_Kong’。
  • normalize:bool,默认值为 False,如果为True的话,那么在产生时间索引值之前会先把start和end都转化为当日的午夜0点。
  • name:str,默认值为None,给返回的时间索引指定一个名字。
  • closed:string 或者 None,默认值为None,表示 start 和 end 这个区间端点是否包含在区间内,可以有三个值,left 表示左闭右开区间,righ 表示左开右闭区间,None 表示两边都是闭区间。
In [11]: import pandas as pd

In [12]: pd.date_range(start='20170101',end='20170110')
Out[12]:
DatetimeIndex(['2017-01-01', '2017-01-02', '2017-01-03', '2017-01-04',
               '2017-01-05', '2017-01-06', '2017-01-07', '2017-01-08',
               '2017-01-09', '2017-01-10'],
              dtype='datetime64[ns]', freq='D')

In [13]: pd.date_range(start='20170101',periods=10)
Out[13]:
DatetimeIndex(['2017-01-01', '2017-01-02', '2017-01-03', '2017-01-04',
               '2017-01-05', '2017-01-06', '2017-01-07', '2017-01-08',
               '2017-01-09', '2017-01-10'],
              dtype='datetime64[ns]', freq='D')

In [14]: pd.date_range(start='20170101',periods=10,freq='1D')
Out[14]:
DatetimeIndex(['2017-01-01', '2017-01-02', '2017-01-03', '2017-01-04',
               '2017-01-05', '2017-01-06', '2017-01-07', '2017-01-08',
               '2017-01-09', '2017-01-10'],
              dtype='datetime64[ns]', freq='D')

In [15]: pd.date_range(start='20170101',end='20170110',freq='3D',name='dt')
Out[15]: DatetimeIndex(['2017-01-01', '2017-01-04', '2017-01-07', '2017-01-10'],
 dtype='datetime64[ns]', name='dt', freq='3D')

In [16]: pd.date_range(start='2017-01-01 08:10:50',periods=10,freq='s',normaliz
    ...: e=True)
Out[16]:
DatetimeIndex(['2017-01-01 00:00:00', '2017-01-01 00:00:01',
               '2017-01-01 00:00:02', '2017-01-01 00:00:03',
               '2017-01-01 00:00:04', '2017-01-01 00:00:05',
               '2017-01-01 00:00:06', '2017-01-01 00:00:07',
               '2017-01-01 00:00:08', '2017-01-01 00:00:09'],
              dtype='datetime64[ns]', freq='S')

In [17]: pd.date_range(start='2017-01-01 08:10:50',end='2017-01-02 09:20:40',fr
    ...: eq='s',normalize=True)
Out[17]:
DatetimeIndex(['2017-01-01 00:00:00', '2017-01-01 00:00:01',
               '2017-01-01 00:00:02', '2017-01-01 00:00:03',
               '2017-01-01 00:00:04', '2017-01-01 00:00:05',
               '2017-01-01 00:00:06', '2017-01-01 00:00:07',
               '2017-01-01 00:00:08', '2017-01-01 00:00:09',
               ...
               '2017-01-01 23:59:51', '2017-01-01 23:59:52',
               '2017-01-01 23:59:53', '2017-01-01 23:59:54',
               '2017-01-01 23:59:55', '2017-01-01 23:59:56',
               '2017-01-01 23:59:57', '2017-01-01 23:59:58',
               '2017-01-01 23:59:59', '2017-01-02 00:00:00'],
              dtype='datetime64[ns]', length=86401, freq='S')

In [18]: pd.date_range(start='2017-01-01 08:10:50',periods=15,freq='s',normaliz
    ...: e=False)
Out[18]:
DatetimeIndex(['2017-01-01 08:10:50', '2017-01-01 08:10:51',
               '2017-01-01 08:10:52', '2017-01-01 08:10:53',
               '2017-01-01 08:10:54', '2017-01-01 08:10:55',
               '2017-01-01 08:10:56', '2017-01-01 08:10:57',
               '2017-01-01 08:10:58', '2017-01-01 08:10:59',
               '2017-01-01 08:11:00', '2017-01-01 08:11:01',
               '2017-01-01 08:11:02', '2017-01-01 08:11:03',
               '2017-01-01 08:11:04'],
              dtype='datetime64[ns]', freq='S')

In [19]: pd.date_range(start='20170101',end='20170110',freq='3D',closed='left')
    ...:
Out[19]: DatetimeIndex(['2017-01-01', '2017-01-04', '2017-01-07'], dtype='dateti
me64[ns]', freq='3D')

In [20]: pd.date_range(start='20170101',end='20170110',freq='3D',closed='right'
    ...: )
Out[20]: DatetimeIndex(['2017-01-04', '2017-01-07', '2017-01-10'], dtype='dateti
me64[ns]', freq='3D')
Update time: 2020-05-25

results matching ""

    No results matching ""